﻿using System.Linq;
using System;
using System.Web;
using LINQExtensions;

namespace EasyLift.TMS.Web.Models
{
    public class VehicleQCStatusRepository
    {
        private CoreBizDBEntities db = new CoreBizDBEntities();

        //Query Methods
        public IQueryable<VehicleQCStatus> GetAllData()
        {
            return from VehicleQCStatus in db.VehicleQCStatuses
                   orderby VehicleQCStatus.VehicleQCStatusCode descending
                   select VehicleQCStatus;
        }


        public IQueryable<VehicleQCStatus> GetDataByName(string searchField, string searchOper, string searchString)
        {
            var ps = db.VehicleQCStatuses.Where(p => p.VehicleQCStatusCode.Contains(searchString));
            if (searchField == "VehicleQCStatusDescription")
                ps = db.VehicleQCStatuses.Where(p => p.VehicleQCStatusDescription.Contains(searchString));
            if (searchField == "VehicleQCStatusCode")
                ps = db.VehicleQCStatuses.Where(p => p.VehicleQCStatusCode.Contains(searchString));
            return ps.AsQueryable();

        }
        public IQueryable<VehicleQCStatus> GetAllData(string sortColumn, string sortOrder, bool _search, string searchField, string searchOper, string searchString)
        {
            if (_search)
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetDataByName(searchField, searchOper, searchString).OrderByDescending(sortColumn);
                    else
                        return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
                else
                    return GetDataByName(searchField, searchOper, searchString).OrderBy(sortColumn);
            }
            else
            {
                if (sortColumn != "")
                    if (sortOrder.ToUpper() == "DESC")
                        return GetAllData().OrderByDescending(sortColumn);
                    else
                        return GetAllData().OrderBy(sortColumn);
                else
                    return GetAllData();
            }
        }



        public VehicleQCStatus GetByID(string id)
        {
            return db.VehicleQCStatuses.SingleOrDefault(VehicleQCStatus => VehicleQCStatus.VehicleQCStatusCode == id);
        }

        //Inset
        public void Add(VehicleQCStatus VehicleQCStatus)
        {
            //if (VehicleQCStatus.StatusID == 0 )
            //    VehicleQCStatus.StatusID = 1;
            VehicleQCStatus.InsertedUser = HttpContext.Current.User.Identity.Name;
            VehicleQCStatus.InsertedDate = DateTime.Now;
            VehicleQCStatus.UpdatedUser = HttpContext.Current.User.Identity.Name;
            VehicleQCStatus.UpdatedDate = DateTime.Now;
            VehicleQCStatus.DataVersion = 1;

            db.VehicleQCStatuses.AddObject(VehicleQCStatus);
        }

        //Update
        public void Update(VehicleQCStatus VehicleQCStatus)
        {

            VehicleQCStatus originalVehicleQCStatus = GetByID(VehicleQCStatus.VehicleQCStatusCode);

            VehicleQCStatus.InsertedUser = originalVehicleQCStatus.InsertedUser;
            VehicleQCStatus.InsertedDate = originalVehicleQCStatus.InsertedDate;
            VehicleQCStatus.UpdatedUser = HttpContext.Current.User.Identity.Name;
            VehicleQCStatus.UpdatedDate = DateTime.Now;
            VehicleQCStatus.DataVersion = ++originalVehicleQCStatus.DataVersion;
  
            db.VehicleQCStatuses.ApplyCurrentValues(VehicleQCStatus);
        }

        //Delete
        public void Delete(VehicleQCStatus VehicleQCStatus)
        {
            db.VehicleQCStatuses.DeleteObject(VehicleQCStatus);
        }

        //Persistance
        public void Save()
        {
            db.SaveChanges();
        }
    }
}